บทความด้านคอมพิวเตอร์

บทความ, องค์ความรู้, การทำเว็บไซต์, ทำโปรแกรม

ค้นหา และศึกษาบทความด้านคอมพิวเตอร์ การเขียนเว็บไซต์ ทำเว็บไซต์ ทำโปรแกรม ทำโปรแกรมบนมือถือ ออกแบบเว็บไซต์ การเขียนโปรแกรม การออกแบบเว็บไซต์ ฐานข้อมูล รวมไปถึงข่าวแวดวง IT, Computer และบทความต่าง ๆ ที่ถูกคัดสรรมาอย่างดี ด้วยเนื้อหาที่สดใหม่ กระชับ และอ่านเข้าใจง่าย ซึ่งจะทำให้คุณสามารถที่จะแก้ไขปัญหาต่าง ๆ จากบทความ และองค์ความรู้ที่ทางเราเลือกมาให้ โดยบทความทุกบทความถูกเขียนขึ้นใหม่ ในเรื่องต่าง ๆ เพื่อนำเสนอมุมมองใหม่ ๆ ให้กับผู้อ่านทุกคน


ประเภทบทความ
บทความด้านคอมพิวเตอร์

    การอ้างอิง Controls จาก Parent Page ไปยัง Web User Control Form

    บทความวันที่ 9 ธันวาคม 2555

    บทความนี้ขอกล่าวถึงวิธีการ การอ้างอิง Controls จาก Parent Page ไปยัง Web User Control Form ซึ่งสามารถอ้างอิง Controls ได้ทุกชนิดที่ปรากฏอยู่ใน Web User Control Form

     

    ภาพรวมของการอ้างอิง Controls จาก Parent Page ไปยัง Web User Control Form

    1. เรียก id ของ Web User Control Form แล้วตามด้วย method FindControl

    2. ทำการ Casting ให้ Controls นั้นเป็นชนิดที่ถูกต้อง เช่น Label, TextBox 

     

    ตัวอย่างโปรแกรม .aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>
    <%@ Register Src="~/WebUserControl1.ascx" TagPrefix="ctl" TagName="control" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <ctl:control runat="server" ID="control" />
        </div>
        </form>
    </body>
    </html>
    

     

    ตัวอย่างโปรแกรม .aspx.cs

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    namespace WebApplication6
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                ((Label)control.FindControl("lbl")).Text = "Hello, My Control Form :)";
    
            }
    
        }
    }
    

     

    ตัวอย่างโปรแกรม .ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication6.WebUserControl1" %>
    
    

     

    ผลลัพธ์

    Hello, My Control Form :)
    

    อ่านเนื้อหาอื่นเพิ่มเติมฟรี ได้ที่
    Devdit - พบคำตอบด้านไอทีและทำตามง่ายๆ



    คำค้นหา การอ้างอิง Controls จาก Parent Page ไปยัง Web User Control Form, รับเขียนเว็บ, รับทำเว็บ, เรียนเขียนโปรแกรม